import 'package:flutter/material.dart';
import 'package:flutter_company_app/model/product.dart';
import 'package:flutter_company_app/services/product.dart';
import 'home/home_banner_widget.dart';
import 'home/home_product_widget.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  ProductListModel listData = ProductListModel.fromJson([]);

  void getProductList() async {
    var data = await getProductResult();
    ProductListModel list = ProductListModel.fromJson(data);
    setState(() {
      // 一次性将所获取的内容全部填充listData中
      listData.data.addAll(list.data);
    });
  }

  @override
  void initState() {
    super.initState();
    getProductList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: [
          // 构建顶部照片轮播
          HomeBannerWidget(),
          //  构建商品列表
          HomeProductWidget(listData),
        ],
      ),
    );
  }
}
